package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import dao.DishDao;
import dao.util.DBConnection;
import po.Dish;

public class DishDaoImpl implements DishDao{
	Connection con = null;
	PreparedStatement ps = null;
	
	@Override
	public List<Dish> queryDish() {
		
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select * from c_dishes";
		List<Dish> listDish = new ArrayList<Dish>();
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				int dId = rs.getInt("dId");
				String dName = rs.getString("dName");
				double dPrice = rs.getDouble("dPrice");
				String dPic = rs.getString("dPic");
				Dish dish = new Dish(dName, dPrice, dPic);
				dish.setdId(dId);
				listDish.add(dish);
			}
			return listDish;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}
	
	@Override
	public Dish queryADish(int dId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select * from c_dishes where dId = ?";
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, dId);
			rs = ps.executeQuery();
			while(rs.next()){
				String dName = rs.getString("dName");
				double dPrice = rs.getDouble("dPrice");
				String dPic = rs.getString("dPic");
				Dish dish = new Dish(dName, dPrice, dPic);
				dish.setdId(dId);
				return dish;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}
	
	@Override
	public int addDish(Dish d) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delDish(int dId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateDish(Dish d) {
		// TODO Auto-generated method stub
		return 0;
	}

	

}
